package com.xms.plugin.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.UpdaterService;
import com.xms.plugin.entity.EntityPlugin;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * EntitypluginUpdaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/15
 */
@CacheConfig(cacheNames = "entityplugin", keyGenerator = "CacheKeyGenerator")
@Service
public class EntityPluginUpdaterService extends UpdaterService<EntityPlugin> implements IEntityPluginUpdaterService {

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(EntityPlugin entity) throws XmsException, SQLException {
        return super.updateById(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateMany(List<EntityPlugin> entity) throws XmsException, SQLException {
        return super.saveBatch(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(Wrapper<EntityPlugin> updateWrapper){
        return super.update(updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(EntityPlugin entity, Wrapper<EntityPlugin> updateWrapper){
        return super.update(entity, updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateState(boolean isEnabled, UUID... id) {
        UpdateWrapper<EntityPlugin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(EntityPlugin::getStatecode, isEnabled)
                .in(EntityPlugin::getEntitypluginid, id);
        return super.update(updateWrapper);
    }
}
